package com.jianglin.controller;

import com.github.pagehelper.PageInfo;
import com.jianglin.model.Building;
import com.jianglin.service.IBuildingService;
import com.jianglin.util.JsonObject;
import com.jianglin.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
/**
 *
 *
 * @author liyijia
 */
@Api(tags = {"楼宇管理"})
@CrossOrigin(origins="http://localhost:5500")
@RestController
@RequestMapping("/building")
public class BuildingController {

    @Resource
    private IBuildingService buildingService;

    @ApiOperation(value = "楼宇分页查询")
    @RequestMapping("/queryBuildAll")
    public JsonObject<Building> queryBuildAll(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "15") Integer limit,
            String numbers) {

        PageInfo<Building> pageInfo = buildingService.findBuildAll(page, limit, numbers);

        JsonObject<Building> object = new JsonObject<>();
        //查询成功设置状态码0
        object.setCode(0);
        object.setMsg("ok");
        //设置数目
        object.setCount(pageInfo.getTotal());
        //设置数据
        object.setData(pageInfo.getList());
        return object;
    }

    @ApiOperation(value = "获取所有楼宇")
    @RequestMapping("/queryBuild")
    public List<Building> queryBuild() {
        PageInfo<Building> pageInfo = buildingService.findBuildAll(1, 100, null);
        return pageInfo.getList();
    }

    @ApiOperation(value = "新增楼宇")
    @PostMapping("/add")
    public R add(@RequestBody Building building) {
        if (building.getNumbers() == null) {
            //增加的金额不能为空
            return R.fail(400, "楼宇不能为空");
        }
        if (building.getUints()== null ) {
            //增加的日期不能为空
            return R.fail(400, "单元不能为空");
        }
        int num = buildingService.add(building);
        //增加成功返回ok
        return num > 0 ? R.ok() : R.fail("添加失败");
    }

    @ApiOperation(value = "批量删除楼宇")

    @PostMapping("/deleteByIds")
    public R delete(String ids) {
        //批量删除楼宇通过，分割
        Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .forEach(buildingService::delete);
        return R.ok();
    }

    @ApiOperation(value = "更新楼宇信息")
    @RequestMapping ("/update")
    public R update(@RequestBody Building building) {
        int num = buildingService.updateData(building);
        return num > 0 ? R.ok() : R.fail("修改失败");
    }

    @ApiOperation(value = "分页查询楼宇列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页数量", defaultValue = "10")
    })
    @GetMapping("/list")
    public PageInfo<Building> list(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        return buildingService.findListByPage(page, size);
    }

    @ApiOperation(value = "按ID查询楼宇")
    @GetMapping("/{id}")
    public Building findById(@PathVariable Long id) {
        return buildingService.findById(id);
    }


}
